<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Derby with Netbeans</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Derby with Netbeans, Java DB">
<meta name="description" content="Derby with Netbeans">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Derby with Netbeans</h1>

<p>
In this chapter, we will work with the Derby database inside the Netbeans IDE. 
Netbeans has a built-in support for the Derby database in its basic Java SE
bundle. 
</p>

<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* NewSquare */
google_ad_slot = "0364418177";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 

<p>
So far we have worked with the Apache Derby distribution. In this chapter, we
will work with Java DB. It is the same database, only under a different name. 
Java DB is shipped with Java distribution from Java 6 version.
</p>

<img src="/img/db/derby/services.png" alt="Netbeans services window">
<div class="figure">Figure: Netbeans services window</div>

<p>
In the Services window, we expand the Databases node. We can see two nodes. 
The Java DB node and the Drivers node. In the Java DB
node we have all databases. In the drivers node, we can see various JDBC 
drivers that we can use to connect to RDBMS. These database drivers come with the
Netbeans IDE. We have Java DB drivers for both embedded and server modes. 
</p>

<img src="/img/db/derby/javadbproperties.png" alt="Java DB Properties Window">
<div class="figure">Figure: Java DB Properties Window</div>

<p>
The above figure is the Java DB Properties dialog window. The window is shown when we 
right click on the Java DB node and select the Properties option. In this dialog we can
set two important settings. The Java DB Installation directory and the Java DB system 
directory. 
</p>

<h2>Creating a database</h2>

<p>
First thing to do is to create a new database. We will create testdb database.
</p>

<img src="/img/db/derby/javadb.png" alt="Java DB context menu">
<div class="figure">Figure: Java DB context menu</div>

<p>
When we right click on the Java DB node, a context menu appears. It has 
four options. Start server, Stop server, Create Database and Properties. 
We choose the Create Database item to create a new database. 
</p>

<img src="/img/db/derby/createdatabase.png" alt="Create Java DB Database dialog">
<div class="figure">Figure: Create Java DB Database dialog</div>

<p>
A dialog pops up. In this dialog window we provide a database name, user name
and password. Note the database location string. It is the Java DB system directory,
where our database files will be created. The default Java DB system directory is the
.netbeans-derby directory located in the home directory.
</p>

<img src="/img/db/derby/testdb.png" alt="New database created">
<div class="figure">Figure: New database created</div>

<p>
At this moment, we have a new database created. It is visually indicated by
a new database icon under the Java DB node. 
</p>

<h2>Database connection</h2>

<p>
After the database is created, we create a database connection. 
</p>


<img src="/img/db/derby/cicon1.png" alt="Disconnected icon">
&nbsp;&nbsp;
<img src="/img/db/derby/cicon2.png" alt="Connected icon">

<p>
Netbeans uses these icons for connection objects. The first icon
is for disconnected database connection object, the second for an established 
database connection object. 
</p>

<img src="/img/db/derby/connection.png" alt="Database connection">

<p>
The above connection is a Java DB connection created with a
Java DB server driver. Note that when we have created the testdb database, 
a Java DB server was automatically started and a connection created. 
New database connections can be created by right clicking on the Java DB driver 
and choosing the Connect Using option.
</p>

<p>
We are going to create an embedded Java DB database connection. Before creating
the connection, we need to stop the Java DB server, if it is running. Java DB database
cannot be booted by a Java DB server and connected to by an embedded driver at
the same time. Note that we did not have to start the server explicitly. The server could 
be started behind the scenes. For example by connecting to the Java DB server connection object or
creating a new database. 
</p>

<img src="/img/db/derby/stopserver.png" alt="Stopping the server">
<div class="figure">Figure: Stopping the server</div>

<p>
We click on the Java DB node with a right mouse button. If the Stop Server
option is enabled it means that the server is running. We select it to stop 
the server. 
</p>

<img src="/img/db/derby/emcon.png" alt="Creating embedded connection">
<div class="figure">Figure: Creating embedded connection</div>

<p>
To create an embedded connection, we right click on Java DB Embedded driver
and select the Connect Using option. Similarly, we create a server connection by
choosing a Java DB Server driver. 
</p>

<img src="/img/db/derby/newconwiz.png" alt="New connection wizard">
<div class="figure">Figure: New connection wizard</div>

<p>
We have a New Connection Wizard dialog. In this dialog, we fill in the database
name and user credentials. The JDBC url is created from this data. We have specified 
the full path to the testdb database. The Java DB system directory seems not to be
taken into account here. 
</p>

<img src="/img/db/derby/embconicon.png" alt="Embedded connection icon">

<p>
After succesfully creating the embedded database connection, we see the above icon
in the Netbeans Services window.
</p>

<h2>Creating a table</h2>

<p>
The database connection is created. The next thing we do is to create
a new database table. We will create a simple table called FRIENDS
with two columns. Id and Name. The Id will be INTEGER and Name VARCHAR(30).
</p>

<p>
We expand the Database Connection node and further expand the 
USER12 schema. We right click on the Table icon and choose Create Table
option. 
</p>

<img src="/img/db/derby/createtable.png" alt="Creating a new table">
<div class="figure">Figure: Creating a new table</div>

<p>
A Create Table dialog appears. We create two columns. Id and
Name. 
</p>

<img src="/img/db/derby/friends.png" alt="Friends table created">
<div class="figure">Figure: Friends table created</div>

<p>
A FRIENDS table has been created. Now the Tables node is expandable
and we see a new table icon. 
</p>

<img src="/img/db/derby/executecommand.png" alt="Executing a command">
<div class="figure">Figure: Executing a command</div>

<p>
Next we are going to execute some SQL statements. We right click on the 
FRIENDS table icon and select Execute Command option. A new SQL Command 
window appears in Netbeans.  
</p>

<img src="/img/db/derby/sqlcommand.png" alt="Inserting data into the FRIENDS table">
<div class="figure">Figure: Inserting data into the FRIENDS table</div>

<p>
In the SQL Command window we write several INSERT INTO SQL statements. 
We execute the statements by clicking on the Run SQL icon. The icon is
a brown oval object with a green triangle. We can execute SQL statements
with a Ctrl + Shift + E shortcut too. 
</p>

<img src="/img/db/derby/viewfriends.png" alt="Viewing FRIENDS data">
<div class="figure">Figure: Viewing FRIENDS data</div>

<p>
Running SELECT * FROM FRIENDS in the SQL Command window we see a new window 
pop up. In this window we have a table widget with our data organized 
in columns and rows. We have icons to modify data in this gui component. 
</p>

<img src="/img/db/derby/icons.png" alt="Icons to modify data">

<p>
The above picture shows the icons to work with the data in the table. 
The first two icons are used to insert a new record and delete
a selected record, respectively. We can delete multiple records if
we select more rows with a mouse pointer and pressing the Shift key
simultaneously. If the data was modified, the Commit Record(s) icon
is enabled. The data is saved only after we commit it with this action.
Except for the SQL statements, we can modify data with GUI tools. 
By double clicking on a record a line widget appears. In this widget,
we can change the data. The changes are saved by clicking on the 
Commit Record(s) action. 
</p>


<hr class="btm">

<p>
In the chapter, we have worked with Java DB inside the Netbeans IDE. 
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified March 3, 2012  <span class="copyright">&copy; 2007 - 2012 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

